﻿Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Runtime.InteropServices



Public Class SyntaxHighlight

    Private mKeywords As String()
    Public Property Keywords() As String()
        Get
            Return mKeywords
        End Get
        Set(ByVal value As String())
            mKeywords = value
        End Set
    End Property

    Private mSyntaxList As LanguageSyntax()
    Public Property SyntaxList() As LanguageSyntax()
        Get
            Return mSyntaxList
        End Get
        Set(ByVal value As LanguageSyntax())
            mSyntaxList = value
        End Set
    End Property

    Private mOptions As RegexOptions
    Public Property Options() As RegexOptions
        Get
            Return mOptions
        End Get
        Set(ByVal value As RegexOptions)
            mOptions = value
        End Set
    End Property

    Private regKeywords As Regex

    <DllImport("User32", EntryPoint:="LockWindowUpdate")> _
   Private Shared Function LockWindowUpdate(ByRef hWndLock As Int32) As Boolean
    End Function

    Public Sub New()
        Options = RegexOptions.Multiline Or RegexOptions.IgnoreCase Or RegexOptions.IgnorePatternWhitespace
        XMLInit()
    End Sub

    Public Sub XMLInit()
        ReDim SyntaxList(1)
        SyntaxList(0).Keywords = "<([\w/]*)([^>]*)>" ' Old Pattern "<([\w/]*)[^\>]*>"
        SyntaxList(0).TextColor = Color.Blue
        SyntaxList(0).Group0 = Color.Red
        SyntaxList(0).Group1 = Color.Maroon

        SyntaxList(1).Keywords = "<!--([^\-]|[\r\n]|-[^\-])*-->"
        SyntaxList(1).TextColor = Color.Gray
        SyntaxList(1).Group0 = Color.Gray
        SyntaxList(1).Group1 = Color.Gray
    End Sub

    Public Sub HightlightText(ByRef rtbox As RichTextBox)
        Try
            Dim currentlocation As Long
            currentlocation = rtbox.SelectionStart
            Randomize()
            LockWindowUpdate(rtbox.Handle)
            rtbox.SelectAll()
            rtbox.SelectionColor = Color.Black
            rtbox.Select(currentlocation, 0)
            For Each syntaxes As LanguageSyntax In SyntaxList
                regKeywords = New Regex(syntaxes.Keywords, Options)
                For Each keywordMatch As Match In regKeywords.Matches(rtbox.Text)
                    'Debug.Write("Group count is " & keywordMatch.Groups.Count)
                    'Debug.WriteLine("Group Line 1 is " & keywordMatch.Groups(keywordMatch.Groups.Count - 1).Value)
                    rtbox.Select(keywordMatch.Index, keywordMatch.Length)
                    rtbox.SelectionColor = syntaxes.TextColor
                    If Not keywordMatch.Groups(1) Is Nothing Then
                        rtbox.Select(keywordMatch.Groups(1).Index, keywordMatch.Groups(1).Length)
                        rtbox.SelectionColor = syntaxes.Group0
                    End If
                    If Not keywordMatch.Groups(2) Is Nothing Then
                        rtbox.Select(keywordMatch.Groups(2).Index, keywordMatch.Groups(2).Length)
                        rtbox.SelectionColor = syntaxes.Group1
                    End If
                Next
            Next

            rtbox.Select(currentlocation, 0)
            rtbox.SelectionColor = Color.Black
        Finally
            LockWindowUpdate(IntPtr.Zero)
        End Try
    End Sub

End Class


Public Structure LanguageSyntax
    Dim Keywords As String
    Dim TextColor As Color
    Dim Group0 As Color
    Dim Group1 As Color
End Structure

